package com.yunxi.core.base;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeGeneratorVersionTwo {


    public static void main(String[] args) {

        //***************************手动填写信息****************************
        // 作者
        String author="Guan";
        // 表名
        String[] tableNames = {"pms_report_partner_day"};

        // 选择代码生成 服务路径
        ////biz
        String servicePath = "biz-center/biz-service";
        String apiPath = "biz-center/biz-api";
        ////basedata
//        String servicePath = "basedata-center/basedata-service";
//        String apiPath = "basedata-center/basedata-api";

        // 业务模块名 比如：order、otd 、vehicle
        String moduleName = "sto";
        //***************************手动填写结束****************************

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);
        //在项目中生成
//        gc.setOutputDir(projectPath + "/biz-center/biz-service/src/main/java");
        gc.setOutputDir(projectPath);
        gc.setAuthor(author);
        gc.setOpen(false);
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        gc.setBaseResultMap(true);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        gc.setIdType(IdType.UUID);
        //是否覆盖文件
        gc.setFileOverride(true);
        mpg.setGlobalConfig(gc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //模块名称
        //命令行输入
        pc.setParent("com.yunxi");
        pc.setModuleName("");
        //设置bean的包
        pc.setEntity("model." + moduleName);
        pc.setMapper("dao." + moduleName);
        pc.setService("service.localService." + moduleName);
        pc.setServiceImpl("service.localService.impl." + moduleName);
        pc.setController("controller." + moduleName);

        // 设置文件生产包路径
        Map<String, String> pathInfo = new HashMap<>();
        pathInfo.put(ConstVal.ENTITY_PATH, servicePath + "/src/main/java/com/yunxi/model/" + moduleName);
        pathInfo.put(ConstVal.SERVICE_PATH,servicePath + "/src/main/java/com/yunxi/service/localService/" + moduleName);
        pathInfo.put(ConstVal.SERVICE_IMPL_PATH,servicePath + "/src/main/java/com/yunxi/service/localService/impl/" + moduleName);
        pathInfo.put(ConstVal.MAPPER_PATH, servicePath + "/src/main/java/com/yunxi/dao/" + moduleName);
        pathInfo.put(ConstVal.XML_PATH, servicePath + "/src/main/resources/com/yunxi/dao/" + moduleName);
        pathInfo.put(ConstVal.CONTROLLER_PATH, "dms-web/src/main/java/com/yunxi/controller/" + moduleName);
        pc.setPathInfo(pathInfo);
        mpg.setPackageInfo(pc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://39.105.213.235:3306/dms?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("pms");
        dsc.setPassword("pms");
        dsc.setDbType(DbType.MYSQL);

        dsc.setTypeConvert(new MySqlTypeConvert() {
            // 自定义数据库表字段类型转换【可选】
            public DbColumnType processTypeConvert(GlobalConfig gc, String fieldType) {
                System.out.println("转换类型：" + fieldType);
                // 注意！！processTypeConvert 存在默认类型转换，如果不是你要的效果请自定义返回、非如下直接返回。
                return (DbColumnType) super.processTypeConvert(gc, fieldType);
            }
        });
        mpg.setDataSource(dsc);

        // 自定义配置 自定义全局参数
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                map.put("jinhao", "#");
                map.put("duole", "$");
                map.put("TablePrefix", moduleName.toUpperCase() + ":");
                map.put("dubboServiceImplPackage", "com.yunxi.service.dubboService.impl." + moduleName);
                map.put("dubboServicePackage", "com.yunxi." + moduleName + ".service");
                map.put("entityVOPackage", "com.yunxi." + moduleName + ".pojo");
                this.setMap(map);
            }
        };

        List<FileOutConfig> focList = new ArrayList<>();

        focList.add(new FileOutConfig("/templates/dubboService.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath +"/"+ apiPath + "/src/main/java/com/yunxi/" + moduleName + "/service/"
                    + tableInfo.getEntityName() + "DubboService" + StringPool.DOT_JAVA;
            }
        });

        focList.add(new FileOutConfig("/templates/dubboserviceImpl.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath +"/"+ servicePath + "/src/main/java/com/yunxi/service/dubboService/impl/"
                    + moduleName + "/"
                    + tableInfo.getEntityName() + "DubboServiceImpl" + StringPool.DOT_JAVA;
            }
        });

        focList.add(new FileOutConfig("/templates/entityVO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath +"/"+ apiPath + "/src/main/java/com/yunxi/" + moduleName + "/pojo/"
                    + tableInfo.getEntityName() + "VO" + StringPool.DOT_JAVA;
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
//        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);//表名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//字段名策略
        //strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        //需要生成的表名
        strategy.setInclude(tableNames);//new String[] { "sys_user","car_staff" }
        //公共字段
        //strategy.setSuperEntityColumns("id");
        //自定义mapper
        //strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
        //自定义service父类
        //strategy.setSuperServiceClass("com.baomidou.demo.TestService");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("pms_");
        strategy.setVersionFieldName("VERSION");
        // 字段填充
        List<TableFill> tableFills = new ArrayList<>();
        tableFills.add(new TableFill("CREATE_BY", FieldFill.INSERT));
        tableFills.add(new TableFill("CREATE_TIME", FieldFill.INSERT));
        tableFills.add(new TableFill("UPDATE_BY", FieldFill.INSERT_UPDATE));
        tableFills.add(new TableFill("UPDATE_TIME", FieldFill.INSERT_UPDATE));
        strategy.setTableFillList(tableFills);

        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        mpg.execute();
    }
}